package myJs.user.workflow.featureSelection.preprocess

import com.softwaremill.quicklens._
import AllCp.PreprocessT
import shared.pojo.WorkflowMission._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool
import myJs.tool.Tool._
import shared.Pojo._

import scala.collection.SeqMap
import myJs.Implicits._

import myJs.cps._

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object TransformCp extends PreprocessT with TransformSoftT {

  case class Props(
      curI: Int,
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      datas: List[EachPreprocessData],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit]
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def transformMethodChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).transformMethod).setTo(v)
        props.datasChange(newPreProcess)
      }

      def glogMethodChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).glogMethod).setTo(v)
        props.datasChange(newPreProcess)
      }

      def glogChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).glog).setTo(v)
        props.datasChange(newPreProcess)
      }

      def removeModuleF =  {
        props.removeModule(props.curI)
      }

      def insertModule = (moduleName: String) => {
        props.insertModule(moduleName)
      }

    }

    useEffect(
      SyncIO{
        Tool.popover
      }.void,
      List()
    )

    val transformMethodMap =
      SeqMap("log2" -> "log2", "ln" -> "ln", "log10" -> "log10", "cube" -> messages("cubeRoot"), "glog" -> "glog")
    val glogMethodMap = SeqMap("custom" -> messages("Customization"), "min" -> messages("TheMinimum"))
    val data = props.datas(props.curI)
    val transformMethod = data.transformMethod
    val glogMethod = data.glogMethod
    val glog = data.glog
    val namePrefix = s"preprocess.datas.${props.curI}"

    fieldset(
      paddingRight := "5px", border := "1px solid silver", margin := "0 2px", borderRadius := "4px",
      className := "northbtn myEachSoft",
      MyToolCp.Props(removeModule = FTool.removeModuleF, insertModule = FTool.insertModule).render,
      legend(s"${messages(nameStr)}"),
      div(
        SelectCp.Props(
          value = transformMethod,
          label = messages("transformationMethod"),
          name = "transformMethods[]",
          optionMap = transformMethodMap,
          onChange = FTool.transformMethodChange
        ).render,
        SelectCp.Props(
          value = glogMethod,
          label = messages("ConstantSettingMethod"),
          name = "glogMethods[]",
          optionMap = glogMethodMap,
          onChange = FTool.glogMethodChange,
          show = (transformMethod == "glog")
        ).render,
        InputCp.Props(
          value = glog,
          show = (transformMethod == "glog" && glogMethod == "custom"),
          label = messages("glogConstant"),
          name = s"${namePrefix}.glog",
          onChange = FTool.glogChange
        ).render
      )
    )

  }

  def cp(
      curI: Int,
      datas: List[EachPreprocessData],
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ) = {
    TransformCp.Props(
      curI = curI,
      datas = datas,
      datasChange = datasChange,
      insertModule = insertModule,
      removeModule = removeModule
    ).render
  }

}
